package com.example.hospital.mapper;

import com.example.hospital.domain.entity.IndicatorCategory;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface IndicatorCategoryMapper {
    @Insert("INSERT INTO indicator_category (category_name) VALUES (#{categoryName})")
    @Options(useGeneratedKeys = true, keyProperty = "categoryId")
    int insert(IndicatorCategory category);

    @Update("UPDATE indicator_category SET category_name=#{categoryName} WHERE category_id=#{categoryId}")
    int update(IndicatorCategory category);

    @Delete("DELETE FROM indicator_category WHERE category_id=#{categoryId}")
    int delete(@Param("categoryId") Long categoryId);

    @Select("SELECT * FROM indicator_category WHERE category_id=#{categoryId}")
    IndicatorCategory findById(@Param("categoryId") Long categoryId);

    @Select({
            "<script>",
            "SELECT * FROM indicator_category",
            "<where>",
            "  <if test=\"categoryName != null and categoryName != ''\"> AND category_name LIKE CONCAT('%', #{categoryName}, '%') </if>",
            "</where>",
            "ORDER BY category_id DESC",
            "LIMIT #{limit} OFFSET #{offset}",
            "</script>"
    })
    List<IndicatorCategory> list(@Param("categoryName") String categoryName, @Param("limit") int limit, @Param("offset") int offset);

    @Select({
            "<script>",
            "SELECT COUNT(1) FROM indicator_category",
            "<where>",
            "  <if test=\"categoryName != null and categoryName != ''\"> AND category_name LIKE CONCAT('%', #{categoryName}, '%') </if>",
            "</where>",
            "</script>"
    })
    long count(@Param("categoryName") String categoryName);
}